<?php
namespace app\admin\controller;

class Album extends Admin{


    public function index(){
        $Album = M('album');
        $count = $Album->count();
        import('@.util.Page');
        $Page = new \Page($count,15);
        $list = $Album->limit($Page->limit)->order('sort desc,id desc')->select();
        return $this->fetch('index',[
            'list'=>$list,
            'page' => $Page -> pageShow()
        ]);
    }

    public function update(){
        $Album = D('Album');
        if($_POST){
            if(!preg_match("/^.{1,100}$/",I('post.aname/s'))){
                return '相册名称长度为1-50个字符';
            }
            $map = ['aname'=>I('post.aname/s'),'id'=>['neq',I('post.aid/d')]];
            if($Album->where($map)->field('id')->find()){
                return '该相册名称已存在';
            }
            $data = [
                'aname' => I('post.aname/s'),
                'status' => I('post.status/d'),
                'update_time' => I('post.update_time')
            ];
            $res = $Album->where(['id'=>I('post.aid/d')])->setField($data);
            return $res ? '1' : '保存失败';
        }else{
            $res = $Album->where(['id'=>I('get.aid/d')])->find();
            if(!$res){
                return $this->myError('没有找到该相册');
            }
            return $this->fetch('update',['res'=>$res]);
        }
    }

    public function create(){
        if(IS_POST){
            if(!preg_match("/^.{1,100}$/",I('post.aname/s'))){
                return '相册名称长度为1-50个字符';
            }
            $Album = M('album');
            if($Album->where(['aname'=>I('post.aname')])->field('id')->find()){
                return '该相册名称已存在';
            }
            $res = $Album->add(I('post.'));
            return $res ? '1' : '保存失败';
        }else{
            return $this->fetch('create');
        }
    }

    public function delete(){
        $Album = D('Album');
        $aid = I('post.sid/d');
        //查询该相册
        $res = $Album->where(['id'=>$aid])->find();
        if(!$res){
            return '该相册不存在';
        }
        //删除该相册下的所有图片
        $Picture = M('Picture');
        $list = $Picture->field('id,path')->where(['album_id'=>$aid])->select();
        $Qiniu = A('qiniu','event');
        foreach($list as $v){
            $Qiniu->delete(substr(strrchr($v['path'],'/'),1));
        }
        $Picture->where(['album_id'=>$aid])->delete();
        $r = $Album->where(['id'=>$aid])->delete();
        return $r ? '1' : '删除相册失败';
    }

    //修改排序
    public function setSort(){
        if(IS_AJAX){
            if(!I('post.aid/d')){
                return '参数错误';
            }
            $Album = D('Album');

            $data = [
                'sort' => I('post.sort/d'),
                'update_time' => I('post.update_time')
            ];
            return $Album->where(['id'=>I('post.aid/d')])->setField($data);
        }
    }

    //获取相册列表
    public function albumList(){
        $Album = M('album');
        return $Album->field('id,aname,status')->select();
    }
}
